package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.ulwx.database.DataBaseSet;
import com.ulwx.database.RowMapper;
import com.ulwx.tool.DbUtils;
import feeinterface.modeldb.LibVerList;
import feeinterface.protocol.utils.Constants;

/**
 * 软件文件表，存放软件的程序下载路径
 * 
 * @author 黄杰
 * 
 */
public class LibVerListDao {
	private static Logger log = Logger.getLogger(LibVerListDao.class); // 跟类名一样

	/**
	 * 通过业务合作ID，手机imsi号码，平台名称，虚拟机版本号码
	 * 
	 * @param coid
	 *            业务合作ID
	 * @param imsi
	 *            手机IMSI卡号
	 * @param platform
	 *            平台名称
	 * @param vmVer
	 *            虚拟机版本号
	 * @param verLib
	 *            需要下载的版本号码
	 * @param IsUpdate
	 *            更新或下载
	 * @param isTest
	 *            是否用于pc模拟器测试 1：是 0：否
	 * @return
	 * @throws Exception
	 */
	public static LibVerList getLibList(Integer coid, Integer cpid,
			String imsi, String platform, Integer vmVer, Integer IsUpdate,
			Integer isTest, Integer verLib) throws Exception {
		LibVerList libVerList = null;
		String updateSql = "UPDATE LibVerList SET DownLoadCounter=DownLoadCounter+1 WHERE LibVerListSno=";
		String sql = "";
		Map<Integer, Object> map = getSimMap(coid, cpid, imsi, platform, vmVer,
				isTest);
		if (IsUpdate == Constants.UpdateOrDown.UPDATE) {
			sql = getSqlStr("")
					+ " ORDER BY LibVer DESC,Seq limit 1";
			map = getSimMap(coid, cpid, imsi, platform, vmVer, isTest);
		} else {
			sql = getSqlStr(" AND LibVer = ? ") + "limit 1";
			map = getSimMap(coid, cpid, imsi, platform, vmVer, isTest, verLib);

		}
		RowMapper<LibVerList> rowMapper = new RowMapper<LibVerList>() {
			public LibVerList mapRow(DataBaseSet rs) throws Exception {
				LibVerList lv = new LibVerList();
				lv.setLibVerListSno(rs.getInt("LibVerListSno"));
				lv.setLibVer(rs.getInt("LibVer"));
				lv.setDownLoadCounter(rs.getInt("DownLoadCounter"));
				lv.setDownloadURl(rs.getString("DownloadURl"));
				return lv;
			}
		};
		List<LibVerList> list = DbUtils
				.doQueryObject(null, sql, map, rowMapper);
		if (list != null && list.size() > 0) {
			libVerList = list.get(0);
			DbUtils.executeBindUpdate(null,
					updateSql + libVerList.getLibVerListSno(), null);
		}
		return libVerList;

	}

	/**
	 * 查找相同条件最大版本号的记录
	 * 
	 * @param coid
	 * @param cpid
	 * @param imsi
	 * @param platform
	 * @param vmVer
	 * @param isTest
	 *            是否用于pc模拟器测试 1：是 0：否
	 * @return
	 * @throws Exception
	 */
	public static LibVerList getLastLibList(Integer coid, Integer cpid,
			String imsi, String platform, Integer vmVer, Integer isTest)
			throws Exception {

		String sql = getSqlStr("") + " ORDER BY LibVer DESC ,Seq limit 1";
		Map<Integer, Object> map = getSimMap(coid, cpid, imsi, platform, vmVer,
				isTest);
		RowMapper<LibVerList> rowMapper = new RowMapper<LibVerList>() {
			public LibVerList mapRow(DataBaseSet rs) throws Exception {
				LibVerList lv = new LibVerList();
				lv.setLibVerListSno(rs.getInt("LibVerListSno"));
				lv.setLibVer(rs.getInt("LibVer"));
				lv.setDownLoadCounter(rs.getInt("DownLoadCounter"));
				lv.setDownloadURl(rs.getString("DownloadURl"));
				return lv;
			}
		};
		List<LibVerList> list = DbUtils
				.doQueryObject(null, sql, map, rowMapper);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;

	}

	/**
	 * 获取Map
	 * 
	 * @param coid
	 * @param cpid
	 * @param imsi
	 * @param platform
	 * @param vmVer
	 * @param isTest
	 * @return
	 * @throws Exception
	 */
	public static Map<Integer, Object> getSimMap(Integer coid, Integer cpid,
			String imsi, String platform, Integer vmVer, Integer isTest,
			Integer verLib) throws Exception {
		String BsPlatformTypeCode = BasePlatformDao.getPlatform(platform);
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		// select1
		map.put(1, imsi);
		map.put(2, coid);
		map.put(3, platform);
		map.put(4, vmVer);
		map.put(5, isTest);
		map.put(6, verLib);
		// select2
		map.put(7, imsi);
		map.put(8, coid);
		map.put(9, BsPlatformTypeCode);
		map.put(10, vmVer);
		map.put(11, isTest);
		map.put(12, verLib);
		// select3
		map.put(13, imsi);
		map.put(14, coid);
		map.put(15, vmVer);
		map.put(16, isTest);
		map.put(17, verLib);
		// select4
		map.put(18, imsi);
		map.put(19, cpid);
		map.put(20, platform);
		map.put(21, vmVer);
		map.put(22, isTest);
		map.put(23, verLib);
		// select5
		map.put(24, imsi);
		map.put(25, cpid);
		map.put(26, BsPlatformTypeCode);
		map.put(27, vmVer);
		map.put(28, isTest);
		map.put(29, verLib);
		// select6
		map.put(30, imsi);
		map.put(31, cpid);
		map.put(32, vmVer);
		map.put(33, isTest);
		map.put(34, verLib);
		// select7
		map.put(35, imsi);
		map.put(36, platform);
		map.put(37, vmVer);
		map.put(38, isTest);
		map.put(39, verLib);
		// select8
		map.put(40, imsi);
		map.put(41, BsPlatformTypeCode);
		map.put(42, vmVer);
		map.put(43, isTest);
		map.put(44, verLib);
		// select9
		map.put(45, imsi);
		map.put(46, vmVer);
		map.put(47, isTest);
		map.put(48, verLib);
		// select10
		map.put(49, coid);
		map.put(50, platform);
		map.put(51, vmVer);
		map.put(52, isTest);
		map.put(53, verLib);
		// select11
		map.put(54, coid);
		map.put(55, BsPlatformTypeCode);
		map.put(56, vmVer);
		map.put(57, isTest);
		map.put(58, verLib);
		// select12
		map.put(59, coid);
		map.put(60, vmVer);
		map.put(61, isTest);
		map.put(62, verLib);
		// select13
		map.put(63, cpid);
		map.put(64, platform);
		map.put(65, vmVer);
		map.put(66, isTest);
		map.put(67, verLib);
		// select14
		map.put(68, cpid);
		map.put(69, BsPlatformTypeCode);
		map.put(70, vmVer);
		map.put(71, isTest);
		map.put(72, verLib);
		// select15
		map.put(73, cpid);
		map.put(74, vmVer);
		map.put(75, isTest);
		map.put(76, verLib);
		// select16
		map.put(77, platform);
		map.put(78, vmVer);
		map.put(79, isTest);
		map.put(80, verLib);
		// select17
		map.put(81, BsPlatformTypeCode);
		map.put(82, vmVer);
		map.put(83, isTest);
		map.put(84, verLib);
		// select18
		map.put(85, vmVer);
		map.put(86, isTest);
		map.put(87, verLib);
		return map;
	}

	/**
	 * 获取Map
	 * 
	 * @param coid
	 * @param cpid
	 * @param imsi
	 * @param platform
	 * @param vmVer
	 * @param isTest
	 * @return
	 * @throws Exception
	 */
	public static Map<Integer, Object> getSimMap(Integer coid, Integer cpid,
			String imsi, String platform, Integer vmVer, Integer isTest)
			throws Exception {
		String BsPlatformTypeCode = BasePlatformDao.getPlatform(platform);
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		// select1
		map.put(1, imsi);
		map.put(2, coid);
		map.put(3, platform);
		map.put(4, vmVer);
		map.put(5, isTest);
		// select2
		map.put(6, imsi);
		map.put(7, coid);
		map.put(8, BsPlatformTypeCode);
		map.put(9, vmVer);
		map.put(10, isTest);
		// select3
		map.put(11, imsi);
		map.put(12, coid);
		map.put(13, vmVer);
		map.put(14, isTest);
		// select4
		map.put(15, imsi);
		map.put(16, cpid);
		map.put(17, platform);
		map.put(18, vmVer);
		map.put(19, isTest);
		// select5
		map.put(20, imsi);
		map.put(21, cpid);
		map.put(22, BsPlatformTypeCode);
		map.put(23, vmVer);
		map.put(24, isTest);
		// select6
		map.put(25, imsi);
		map.put(26, cpid);
		map.put(27, vmVer);
		map.put(28, isTest);
		// select7
		map.put(29, imsi);
		map.put(30, platform);
		map.put(31, vmVer);
		map.put(32, isTest);
		// select8
		map.put(33, imsi);
		map.put(34, BsPlatformTypeCode);
		map.put(35, vmVer);
		map.put(36, isTest);
		// select9
		map.put(37, imsi);
		map.put(38, vmVer);
		map.put(39, isTest);
		// select10
		map.put(40, coid);
		map.put(41, platform);
		map.put(42, vmVer);
		map.put(43, isTest);
		// select11
		map.put(44, coid);
		map.put(45, BsPlatformTypeCode);
		map.put(46, vmVer);
		map.put(47, isTest);
		// select12
		map.put(48, coid);
		map.put(49, vmVer);
		map.put(50, isTest);
		// select13
		map.put(51, cpid);
		map.put(52, platform);
		map.put(53, vmVer);
		map.put(54, isTest);
		// select14
		map.put(55, cpid);
		map.put(56, BsPlatformTypeCode);
		map.put(57, vmVer);
		map.put(58, isTest);
		// select15
		map.put(59, cpid);
		map.put(60, vmVer);
		map.put(61, isTest);
		// select16
		map.put(62, platform);
		map.put(63, vmVer);
		map.put(64, isTest);
		// select17
		map.put(65, BsPlatformTypeCode);
		map.put(66, vmVer);
		map.put(67, isTest);
		// select18
		map.put(68, vmVer);
		map.put(69, isTest);
		return map;
	}

	/**
	 * 获取SQL语句
	 * 
	 * @param addStr
	 * @return
	 */
	private static String getSqlStr(String addStr) {
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("SELECT *,1 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForCoid=?                 ");
			sb.append("AND Platform=? AND Vmver=? AND IsForPcTest=? " + addStr
					+ " UNION                          ");
			sb.append("SELECT *,2 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForCoid=?                 ");
			sb.append("AND BsPlatformTypeCode=? AND (Platform IS NULL OR Platform='')                             ");
			sb.append("AND Vmver=? AND IsForPcTest=? " + addStr
					+ " UNION                                         ");
			sb.append("SELECT *,3 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForCoid=?                 ");
			sb.append("AND BsPlatformTypeCode=0 AND Vmver=? AND IsForPcTest=? "
					+ addStr + "  UNION               ");
			sb.append("SELECT *,4 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForServiceCpID=?          ");
			sb.append("AND ApplyForCoid=0 AND Platform=? AND Vmver=? AND IsForPcTest=? "
					+ addStr + "  UNION      ");
			sb.append("SELECT *,5 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForServiceCpID=?          ");
			sb.append("AND ApplyForCoid=0 AND BsPlatformTypeCode=? AND (Platform IS NULL OR Platform='')          ");
			sb.append("AND Vmver=? AND IsForPcTest=? " + addStr
					+ "  UNION                                        ");
			sb.append("SELECT *,6 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForServiceCpID=? AND      ");
			sb.append("ApplyForCoid=0 AND BsPlatformTypeCode=0 AND Vmver=? AND IsForPcTest=? "
					+ addStr + " UNION ");
			sb.append("SELECT *,7 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForServiceCpID=0          ");
			sb.append("AND Platform=? AND Vmver=? AND IsForPcTest=? " + addStr
					+ "  UNION                         ");
			sb.append("SELECT *,8 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForServiceCpID=0          ");
			sb.append("AND BsPlatformTypeCode=? AND (Platform IS NULL OR Platform='') AND Vmver=?                 ");
			sb.append("AND IsForPcTest=? "
					+ addStr
					+ "  UNION                                                    ");
			sb.append("SELECT *,9 AS Seq FROM LibVerList WHERE ApplyForIMSI=?  AND ApplyForServiceCpID=0          ");
			sb.append("AND BsPlatformTypeCode=0 AND Vmver=? AND IsForPcTest=? "
					+ addStr + "  UNION               ");
			sb.append("SELECT *,10 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForCoid=?                ");
			sb.append("AND Platform=? AND Vmver=? AND IsForPcTest=? " + addStr
					+ "  UNION                         ");
			sb.append("SELECT *,11 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForCoid=? AND            ");
			sb.append("BsPlatformTypeCode=? AND Platform IS NULL AND Vmver=? AND IsForPcTest=? "
					+ addStr + " UNION ");
			sb.append("SELECT *,12 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForCoid=?                   ");
			sb.append("AND BsPlatformTypeCode=0 AND Vmver=? AND IsForPcTest=? "
					+ addStr + "  UNION                 ");
			sb.append("SELECT *,13 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForServiceCpID=?            ");
			sb.append("AND ApplyForCoid=0 AND Platform=? AND Vmver=? AND IsForPcTest=? "
					+ addStr + "  UNION        ");
			sb.append("SELECT *,14 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForServiceCpID=?            ");
			sb.append("AND ApplyForCoid=0 AND BsPlatformTypeCode=? AND (Platform IS NULL OR Platform='')            ");
			sb.append("AND Vmver=? AND IsForPcTest=? " + addStr
					+ "  UNION                                          ");
			sb.append("SELECT *,15 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForServiceCpID=? AND        ");
			sb.append("ApplyForCoid=0 AND BsPlatformTypeCode=0 AND Vmver=? AND IsForPcTest=? "
					+ addStr + " UNION   ");
			sb.append("SELECT *,16 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForServiceCpID=0            ");
			sb.append("AND Platform=? AND Vmver=? AND IsForPcTest=? " + addStr
					+ "  UNION                           ");
			sb.append("SELECT *,17 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForServiceCpID=0            ");
			sb.append("AND BsPlatformTypeCode=? AND (Platform IS NULL OR Platform='') AND Vmver=?                   ");
			sb.append("AND IsForPcTest=? "
					+ addStr
					+ " UNION                                                       ");
			sb.append("SELECT *,18 AS Seq FROM LibVerList WHERE ApplyForIMSI='' AND ApplyForServiceCpID=0            ");
			sb.append("AND BsPlatformTypeCode=0 AND Vmver=? AND IsForPcTest=? "+ addStr + "                          ");
		} catch (Exception e) {
			log.error("", e);
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
